<?php
declare(strict_types=1);

// +----------------------------------------------------------------------
// | 兔兔答题考试系统
// +----------------------------------------------------------------------
// | 感谢使用兔兔答题考试系统
// | 本系统经过商业授权，不能转售、开源或者以其他方式进行使用，只可在购买协议范围内容使用
// | 若发现未被版权协议，将追究相应的公司、团队、企业等
// | 访问官网：https://www.tutudati.com
// | 兔兔答题考试系统开发者版权所有 拥有最终解释权
namespace App\Admin\Controllers\Audio;

use App\Library\SnowFlakeId;
use App\Models\Audio\Admin\AudioResource;
use App\Models\Audio\Admin\AudioTag;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class AudioResourceController extends AdminController
{
    protected $title = "音频资源";

    public function grid(): Grid
    {
        $grid = new Grid(new AudioResource());
        $grid->model()->orderByDesc("id");
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->like('title', "音频名称");
                $filter->equal('is_show', "上架状态")->radio([
                    1 => '上架',
                    2 => '下架',
                ]);
                $filter->equal('is_top', "置顶状态")->radio([
                    1 => '是',
                    2 => '否',
                ]);
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->between('created_at', "创建时间")->datetime();
                $filter->equal("tag_uid", "音频标签")->select(AudioTag::getTagAll());
            });
        });

        $grid->column('uid', "数据编号")->copyable();
        $grid->column("tag.title", "音频标签");
        $grid->column("title", "音频名称");
        $grid->column("cover", "音频封面")->lightbox(['width' => 50, 'height' => 50]);
        $grid->column("audio_full_path", "音频地址")->display(function ($audio_full_path) {
            return "<a href=" . $audio_full_path . " target='_blank'>点击播放</a>";
        });
        $grid->column("time", "音频时长");
        $grid->column("file_size", "音频大小")->sortable();
        $grid->column("score", "下载积分")->sortable();
        $grid->column("download", "下载次数")->sortable();
        $grid->column("collection", "收藏次数")->sortable();
        $grid->column("orders", "显示权重")->sortable();
        $grid->column('is_show', "上架状态")->display(function ($is_show) {
            if ($is_show == 1) {
                return "<span style='color:#059B50'>上架</span>";
            }
            return "<span style='color:#E70F0FFF'>下架</span>";
        });
        $grid->column('is_top', "置顶状态")->display(function ($is_show) {
            if ($is_show == 1) {
                return "<span style='color:#059B50'>是</span>";
            }
            return "<span style='color:#E70F0FFF'>否</span>";
        });
        $grid->column("created_at", "创建时间");
        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableView();
        });

        return $grid;
    }

    public function form(): Form
    {
        $form = new Form(new AudioResource());
        $form->hidden("uid", "数据编号")->default(SnowFlakeId::getId());
        $form->hidden("url", "附件域名地址")->default(env("QINIU_URL"));
        $form->select("tag_uid", "音频标签")->options(AudioTag::getTagAll())->required();
        $form->file("path", "音频文件")->uniqueName()->required();
        $form->image("image", "音频封面")->uniqueName()->required();
        $form->text("title", "音频名称")->required();
        $form->text("remark", "音频简介");
        $form->time("time", "音频时长")->required();
        $form->number("download", "下载次数")->default(0)->required();
        $form->number("collection", "收藏次数")->default(0)->required();
        $form->number("score", "所需积分")->default(0.00)
            ->rules("required|regex:/^\d*(\.\d{1,2})?$/")
            ->required()
            ->help("用户端下载该资源所需积分，0表示免费。");
        $form->number("orders", "显示顺序")->required()->default(0)->help("值越大显示越排前。");
        $form->radio("is_show", "上架状态")->options([1 => "上架", 2 => "下架"])->default(1);
        $form->radio("is_top", "置顶状态")->options([1 => "是", 2 => "否"])->default(2);

        return $form;
    }
}
